// pages/extrasList/extrasList.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: null,
    identity: null,
    categoryIndex: null,
    regionIndex: null,
    timeIndex: null,
    typeIndex: null,
    key: 2,
    isClick: true,
    categories: [],
    regions: [],
    times: [],
    types: ['公司直招', '个人雇主'],
    list: []
  },

  // 雇主信用
  credit: function (e) {
    this.setData({
      key: e.currentTarget.dataset.key
    })
    this.list(this.data.identity, this.data.type || '', this.data.regions)
  },

  // 最新外快
  newest: function (e) {
    this.setData({
      key: e.currentTarget.dataset.key
    })
    this.list(this.data.identity, this.data.type || '', this.data.regions)
  },

  // 更多筛选
  more: function () {

    this.setData({
      isClick: !this.data.isClick
    })
  },

  // 阻止蒙层滑动
  myCatchTouch: function () {
    return
  },

  // 选择类型
  chooseCategory: function (e) {
    if (this.data.categoryIndex == e.target.dataset.index) {
      this.setData({
        categoryIndex: null,
        type: ''
      })
    } else {
      this.setData({
        categoryIndex: e.target.dataset.index
      })
    }
  },

  // 选择地区
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      regions: e.detail.value
    })
  },

  // 选择时间
  chooseTime: function (e) {
    if (this.data.timeIndex == e.target.dataset.index) {
      this.setData({
        timeIndex: null,
        type: ''
      })
    } else {
      this.setData({
        timeIndex: e.target.dataset.index
      })
    }
  },

  // 选择雇主类型
  chooseType: function (e) {
    if (this.data.typeIndex == e.target.dataset.index) {
      this.setData({
        typeIndex: null,
        type: ''
      })
    } else {
      this.setData({
        typeIndex: e.target.dataset.index
      })
    }
  },

  // 筛选确定
  sure: function () {
    this.list(this.data.identity, this.data.categoryIndex == null ? this.data.type : this.data.categories[this.data.categoryIndex].wt_name, this.data.regions)
    this.setData({
      isClick: !this.data.isClick
    })
  },

  // 关闭蒙层
  closeMask: function () {
    this.setData({
      isClick: !this.data.isClick
    })
  },

  // 外快详情
  toDetail: function (e) {
    if (this.data.identity == 1) {
      wx.navigateTo({
        url: `/pages/extrasDetail/extrasDetail?wid=` + e.currentTarget.dataset.wid,
      })
    } else {
      wx.navigateTo({
        url: '/pages/resumeDetail/resumeDetail?rid=' + e.currentTarget.dataset.rid,
      })
    }
  },

  // 搜索
  search: function (e) {
    this.list(this.data.identity, e.detail.value)
  },

  // 列表
  list: function (identity, type, regions = []) {
    if (identity == 1) {
      app.post('Index/searchWork', {
        args: type,
        sort: this.data.key || '',
        province: regions[0] || '',
        city: regions[1] || '',
        area: regions[2] || '',
        time: this.data.times[this.data.timeIndex] || '',
        state: this.data.types[this.data.typeIndex] == '个人雇主' ? 2 : this.data.types[this.data.typeIndex] == '公司直招' ? 3 : ''
      }).then(res => {
        this.setData({
          list: res.data.result_data,
          type: type,
          identity: identity
        })
      }).then(() => {
        app.post('Common/workTime').then(res => {
          this.setData({
            times: res.data.result_data
          })
        })
      })
    } else {
      app.post('Index/searchResume', {
        args: type,
        sort: this.data.key,
        province: regions[0] || '',
        city: regions[1] || '',
        area: regions[2] || '',
        time: this.data.times[this.data.timeIndex] || '',
      }).then(res => {
        let obj = res.data.result_data
        obj.map((item, index) => {
          if (item.r_args2 != '') {
            let str = item.r_args2.split(',')
            obj[index].r_args2 = str
          }
        })
        console.log(obj);

        this.setData({
          list: obj,
          type: type,
          identity: identity
        })
      }).then(() => {
        app.post('Common/resumeItem').then(res => {
          this.setData({
            times: res.data.result_data
          })
        })
      })
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let identity = wx.getStorageSync('identity')
    let type = options.type
    console.log(options);

    wx.showLoading({
      title: '正在加载中...',
    })
    app.post('Common/workType').then(res => {
      this.setData({
        categories: res.data.result_data,
        // regions:[wx.getStorageSync('province'),wx.getStorageSync('city'),wx.getStorageSync('district')]
      })
    }).then(() => {
      this.list(identity, type)
    })

    wx.hideLoading()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})